package fr.ul.ade.prime.exception;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLConnection;

import fr.ul.ade.prime.analyzer.XmlFetcher;

public class AAAParseException extends AAAException {

	private static final long serialVersionUID = 2566718362607173811L;

	public AAAParseException(Exception e, XmlFetcher fechter) {
		super(retrieveXml(fechter), e);
	}

	public AAAParseException(Exception e, URLConnection conn) {
		super(retrieveXml(conn), e);
	}

	private static String retrieveXml(XmlFetcher fetcher) {
		if (fetcher == null) {
			return "Failed to retrieve all xml";
		}
		return fetcher.getRawXml();
	}

	private static String retrieveXml(URLConnection conn) {
		StringBuilder xml = new StringBuilder();
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					conn.getInputStream()));
			String line = reader.readLine();
			while (line != null) {
				xml.append(line);
				line = reader.readLine();
			}
		} catch (IOException ioe) {
			xml.append("\n>>Failed to retrieve all xml<<");
		}
		return xml.toString();
	}

}
